const { Application } = require("ee-core");
const { app, BrowserWindow, ipcMain, screen } = require("electron");

class Index extends Application {
  constructor() {
    super();
    // this === eeApp;
  }

  /**
   * core app have been loaded
   */
  async ready() {
    // do some things

    /* 自定义操作事件 */
    // 关闭窗口
    ipcMain.on("close", () => {
      const win = this.electron.mainWindow;
      win.close();
      // exec("shutdown /s /t 0");
    });
    // 最大化窗口
    ipcMain.on("max", () => {
      const win = this.electron.mainWindow;
      if (win.isMaximized()) {
        win.unmaximize()
        } else {
          win.maximize()
        }
      // win.maximize();
    });
    // 最小化窗口
    ipcMain.on("min", () => {
      const win = this.electron.mainWindow;
      win.minimize();
    });
    // 向下还原
    ipcMain.on("restoreDown", () => {
      const win = this.electron.mainWindow;
      win.unmaximize();
    });
    // 退出全屏
    ipcMain.on("exitFullScreen", () => {
      const win = this.electron.mainWindow;
      win.setFullScreen(false);
    });
  }

  /**
   * electron app ready
   */
  async electronAppReady() {
    // do some things
  }

  /**
   * main window have been loaded
   */
  async windowReady() {
    // do some things
    // 延迟加载，无白屏
    const winOpt = this.config.windowsOption;
    if (winOpt.show == false) {
      const win = this.electron.mainWindow;
      win.once("ready-to-show", () => {
        win.show();
        win.focus();
      });
    }
  }

  /**
   * before app close
   */
  async beforeClose() {
    // do some things
  }
}

Index.toString = () => "[class Index]";
module.exports = Index;
